﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
namespace TrainingWorkshop.DAL_EF
{
    public class CourseRepository_EF : ICourseRepository_EF
    {
        /// <summary>
        /// Inherit from ICourseRepository_EF to Get All Courses from database 
        /// </summary>
        /// <returns>List of CourseShows</returns>
        public List<TrainingWorkshop.Models.CourseShows> GetAllCourses()
        {
            List<Course> courseList = new List<Course>();
            using (var projectbEntity = new projectbEntities())
            {
                var courseShows = from ecourse in projectbEntity.Courses.Include("Teacher").Include("Level")
                                 select ecourse;
                courseList = courseShows.ToList();
            }
            return Translator.CourseTranslator.TransfertoModelCourseList(courseList);
        }

        /// <summary>
        /// Get selected courses based on coursename and coursestatus
        /// </summary>
        /// <param name="courseName">string coursename</param>
        /// <param name="courseStatus">bool coursestatus</param>
        /// <returns>Model courseshows</returns>
        public List<TrainingWorkshop.Models.CourseShows> GetAllSelectedCourses(string courseName, bool courseStatus)
        {
            List<Course> courseList = new List<Course>();
            using (var projectbEntity = new projectbEntities())
            {
                var courseShows = from course in projectbEntity.Courses.Include("Level").Include("Teacher")
                                 where course.CourseName.Contains(courseName) && course.CourseStatus == courseStatus//contains() is to support fuzzy search
                                 select course;
                courseList = courseShows.ToList();
            }
            return Translator.CourseTranslator.TransfertoModelCourseList(courseList);
        }

        /// <summary>
        /// update selected course
        /// </summary>
        /// <param name="course">model course</param>
        public void UpdateCourse(TrainingWorkshop.Models.Course course)
        {
            using (var projectbEntity = new projectbEntities())
            {
                Course updatedCourse = projectbEntity.Courses.SingleOrDefault(m => m.CourseId == course.CourseId);
                updatedCourse = Translator.CourseTranslator.TransfertoEntityCourse(course, updatedCourse);
                if (updatedCourse != null)
                {
                    projectbEntity.SaveChanges();
                }
            }
        }
        /// <summary>
        /// delete the selected course based on courseId
        /// </summary>
        /// <param name="courseId">int courseId</param>
        public void DeleteCourse(int courseId)
        {
            using (var projectbEntity = new projectbEntities())
            {
                Course deletedCourse = projectbEntity.Courses.SingleOrDefault(m => m.CourseId == courseId);
                projectbEntity.Courses.DeleteObject(deletedCourse);
                projectbEntity.SaveChanges();
            }
        }

        /// <summary>
        /// change coursestatus from open to closed
        /// </summary>
        /// <param name="course">model course</param>
        public void ChangeState(TrainingWorkshop.Models.Course course)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                using (var projectbEntity = new projectbEntities())
                {
                    Course changestateCourse = projectbEntity.Courses.SingleOrDefault(m => m.CourseId == course.CourseId);
                    if (changestateCourse != null)
                    {
                        changestateCourse.CourseStatus = false;
                        projectbEntity.SaveChanges();
                    }
                }
                transactionScope.Complete();
            }
        }
    }
}
